package com.markerhub.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.*;
import com.markerhub.mapper.SysCourseMapper;
import com.markerhub.service.SysCourseLocationService;
import com.markerhub.service.SysCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.markerhub.service.SysCourseTimeService;
import com.markerhub.service.SysLocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wang
 * @since 2022-03-31
 */
@Service
public class SysCourseServiceImpl extends ServiceImpl<SysCourseMapper, SysCourse> implements SysCourseService {


    @Autowired
    SysCourseTimeService sysCourseTimeService;

    @Autowired
    SysCourseLocationService sysCourseLocationService;

    @Autowired
    SysLocationService sysLocationService;

    @Override
    public SysCourse Find(SysCourse sysCourse){
        if(sysCourse.getId()==null)return null;
        sysCourse = getOne(new QueryWrapper<SysCourse>().eq("id",sysCourse.getId()).eq("statu",0));
        return sysCourse;
    }

    @Override
    public SysCourse getObjectByCourseId(String courseId) {
        return getOne(new QueryWrapper<SysCourse>().eq("course_id",courseId));
    }

    @Override
    public String checkDuplicateId(SysCourse sysCourse){
        String CourseId = sysCourse.getCourseId();
        SysCourse Fat = this.getOne(new QueryWrapper<SysCourse>().likeRight("course_id",CourseId).orderByDesc("id").last("limit 1"));
        if(Fat!=null){
            String f= Fat.getCourseId();
            if(f.contains(".")) {
                String[] split = f.split("\\.");
                Integer a = Integer.valueOf(split[1])+1;
                CourseId=CourseId+"."+String.format("%02d", a);
                return CourseId;
            }
            else{
                Fat.setCourseId(f+".01");
                this.updateById(Fat);
                CourseId=CourseId+"."+String.format("%02d", 2);
                return CourseId;
            }
        }
        else
            return null;
    }

    @Override
    public boolean checkTeacherBusy(SysCourse sysCourse) {
        String teacherId = sysCourse.getTeacherId();
        List<SysCourse> sysCourses = this.list(new QueryWrapper<SysCourse>().eq("teacher_id", teacherId));
        if (sysCourse.getTime() == null) return true;
        for (SysCourse i : sysCourses) {
            Set<Long> longs = sysCourse.getTime().keySet();
            Long id = i.getId();
            List<SysCourseTime> check = sysCourseTimeService.list(new QueryWrapper<SysCourseTime>().eq("course_id", id));
            for (SysCourseTime j : check) {
                if (longs.contains(j.getTimeId())) {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public  boolean checkLocationBusy(SysCourse sysCourse){
        SysLocation location = sysCourse.getLocation();
        if(location==null)return true;
        Long id = location.getId();
        if (sysCourse.getTime() == null) return true;
        Set<Long> longs = sysCourse.getTime().keySet();
        List<SysCourseLocation> sysCourseLocations = sysCourseLocationService.list(new QueryWrapper<SysCourseLocation>().eq("location_id", id));
        for (SysCourseLocation i:sysCourseLocations){
            List<SysCourseTime> check = sysCourseTimeService.list(new QueryWrapper<SysCourseTime>().eq("course_id", i.getCourseId()));
            for (SysCourseTime j : check) {
                if (longs.contains(j.getTimeId())) {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public SysCourse process(SysCourse sysCourse){
        if(sysCourse.getTimeId()!=null){
            List<Long> timeId = sysCourse.getTimeId();
            Map<Long,String>time=new TreeMap<>();
            for (Long i:timeId){
                time.put(i,"");
            }
            sysCourse.setTime(time);
        }
        if(sysCourse.getLocationId()!=null){
            SysLocation l = sysLocationService.getById(sysCourse.getLocationId());
            sysCourse.setLocation(l);
        }
        if(sysCourse.getSemesterId()!=null){
            List<Long> semesterId = sysCourse.getSemesterId();
            Map<Long,String>semester=new TreeMap<>();
            for (Long i:semesterId){
                semester.put(i,"");
            }
            sysCourse.setSemester(semester);
        }
        if(sysCourse.getMajorId()!=null){
            List<Long> majorId = sysCourse.getMajorId();
            Map<Long,String>major=new TreeMap<>();
            for (Long i:majorId){
                major.put(i,"");
            }
            sysCourse.setMajor(major);
        }
        return sysCourse;
    }

    @Override
    public String getCourseNameById(Long id) {
        return getOne(new QueryWrapper<SysCourse>().eq("id",id)).getCourseName();
    }


    @Override
    public boolean crash(Long id, SysUserCourse chooseCourse) {//要选的这个课与已选的课时间是否冲突
        boolean found = false;

        //获取要选的这门课的时间列表
        List<Long> times=new ArrayList<Long>();
        List<SysCourseTime> sysCourseTimes=sysCourseTimeService.list(new QueryWrapper<SysCourseTime>().eq("course_id",id));
        for(int i=0;i<sysCourseTimes.size();i++){
            times.add(i,sysCourseTimes.get(i).getTimeId());
        }
        //获取已选课的时间列表
        List<Long> times1=new ArrayList<Long>();
        List<SysCourseTime> sysCourseTimes1=sysCourseTimeService.list(new QueryWrapper<SysCourseTime>().eq("course_id",chooseCourse.getCourseId()));
        for(int i=0;i<sysCourseTimes1.size();i++){
            times1.add(i,sysCourseTimes1.get(i).getTimeId());
        }

        for(int i=0;i<times.size();i++){
            if(times1.contains(times.get(i))){
                found=true;
            }
        }

        return found;
    }


}
